package jun.cli;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TestCli {

	public static void main(String[] args) {
		String ip = "127.0.0.1";
		int port = 9001;
		Socket socket = null;
		try {
			// 创建Socket对象，连接
			socket = new Socket(ip, port);
			System.out.println("客户端启动成功, ip:"+ip+" 端口:"+port);
			
			// 从控制台获取数据
			BufferedReader sytIn = new BufferedReader(new InputStreamReader(System.in));
			
			// 创建PrintWriter对象，传输对象给server
			PrintWriter writer = new PrintWriter(socket.getOutputStream());

			// 获取从server传输过来的数据
			BufferedReader serIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			String line = sytIn.readLine();
			
			while(!"end".equals(line)){
				writer.println(line);
				writer.flush();
				System.out.println("client发送数据:"+line);
				
				System.out.println("接收到server返回的数据:"+serIn.readLine());
				
				line = sytIn.readLine();
			}
			
			socket.close();
			sytIn.close();
			writer.close();
			serIn.close();
			
		} catch (Exception e) {
			System.out.println("客户端启动失败，端口:"+port+" Error:"+e);
		}
	}
}